import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:xlsk/api/model/index.dart';
import 'package:xlsk/routes/xlsk_router.dart';
import 'package:xlsk/utils/dimension.dart';

class Index extends StatefulWidget {

  @override
  _Index createState() => new _Index();
}

class _Index extends State {
  List kechengList = [];
  List kechengList_rel = [];
  List Tongzhi = [];

  @override
  void initState() {
    super.initState();
    print('init_index');
    this.init();
  }

  @override
  Widget build(BuildContext context) {

    return new Scaffold(
        appBar: new AppBar(
          title: new Text('小鹭搜课',style: TextStyle(
              color: Color(0xFF1F2329),
              fontWeight: FontWeight.bold,
              fontSize:Dimension(context).toPx(17),
            ),
          ),
          centerTitle: true,
          backgroundColor: Colors.white,
          elevation: 0,
        ),
        body: new ListView(
          children: [
            new Container(
              child: new Column(
                children: [
                  Notice(Tongzhi), // 头部通知栏
                  Card(),
                  Grid(),
                  classRemind(kechengList,kechengList_rel)
                ],
              ),
              width: Dimension(context).toVw(100),
              constraints: BoxConstraints(
                  minHeight: Dimension(context).toVh(100)
              ),
              decoration: new BoxDecoration(
                  gradient: new LinearGradient(
                      begin: const Alignment(0, -1),
                      end: const Alignment(0, 0.2),
                      colors: [
                        const Color(0xFFFFFFFF),
                        const Color(0xFFF7F8FA)
                      ]
                  )
              ),
            ),
          ],
        )
    );
  }

  void init() async{
    Map<String,dynamic> tongzhiData = {
      "page":1,
      "status":1
    };
    var _kechengList = [],
        _Tongzhi = [];
    await IndexApi().getKecheng(context, '/kecheng').then((res) async{
      if(res['code'] == 200){
        _kechengList = res['data'];
        _Tongzhi = await IndexApi().getTongzhi(context, '/tongzhi', tongzhiData);
      }
    });
    setState(() {
      kechengList = _kechengList;
      kechengList_rel = _kechengList;
      Tongzhi = _Tongzhi;
    });
  }

  @override
  void dispose() {
    super.dispose();
  }
}

class Notice extends StatefulWidget {
  List Tongzhi;
  Notice(this.Tongzhi);

  @override
  _NoticeState createState() => new _NoticeState();
}

class Card extends StatefulWidget {
  @override
  _CardState createState() => new _CardState();
}

class Grid extends StatefulWidget {
  @override
  _GridState createState() => _GridState();
}

class classRemind extends StatefulWidget {
  List kechengList;
  List kechengList_rel;
  classRemind(this.kechengList,this.kechengList_rel);

  @override
  _ClassRemind createState() => _ClassRemind();
}

class _NoticeState extends State<Notice>{
  late SwiperController _controller;

  @override
  void initState() {
    super.initState();
    _controller = new SwiperController();
  }

  @override
  Widget build(BuildContext context){
    List noticeList = [{'title':'暂无待办通知'}];
    if(widget.Tongzhi.length > 0){
      noticeList = widget.Tongzhi;
    }
    return new Container(
      child: new Stack(
        children: [
          new Container(
            child: new Row(
              children: <Widget>[
                Image.asset('assets/images/index-notice.png',
                    width: Dimension(context).toPx(16),
                    height: Dimension(context).toPx(16)),
                Container(
                  child: Text('待办',style: TextStyle(
                      fontSize: Dimension(context).toPx(14),
                      fontWeight: FontWeight.bold,
                      color: Color(0xFF1F2329),
                    ),
                  ),
                  margin: EdgeInsets.only(left:Dimension(context).toPx(8)),
                ),
                Container(
                  width: Dimension(context).toPx(1),
                  height: Dimension(context).toPx(12),
                  margin: EdgeInsets.only(left: Dimension(context).toPx(12)),
                  decoration: new BoxDecoration(
                      color: Color(0xFFC0C4CC)
                  ),
                ),
                GestureDetector(
                  child: Container(
                    child: new Swiper(
                      itemCount: noticeList.length,
                      itemBuilder: (BuildContext context,int index){
                        return new Text('${noticeList[index]['title']}',
                          style: TextStyle(color: Color(0xFF646A73),fontSize: Dimension(context).toPx(13)),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        );
                      },
                      scrollDirection: Axis.vertical,
                      autoplay: noticeList.length>1?true:false,
                      controller: _controller,
                    ),
                    width: Dimension(context).toPx(200),
                    height: Dimension(context).toPx(40),
                    margin: EdgeInsets.only(left: Dimension(context).toPx(12)),
                    padding: EdgeInsets.symmetric(vertical: Dimension(context).toPx(10)),
                  ),
                  behavior: HitTestBehavior.opaque,
                  onTap: (){
                    // _controller.move(2);
                    XlskRouter.go(context, '/qinjia');
                  },
                ),
              ],
            ),
            width: Dimension(context).toPx(349),
            height: Dimension(context).toPx(40)
          ),
          new Positioned(
            child: new Icon(
                Icons.chevron_right,
                color: Color(0xFF646A73),
                size: Dimension(context).toPx(24)
            ),
            top: Dimension(context).toPx(8),
            right: 0,
          )
        ],
      ),
      decoration: new BoxDecoration(
        color: Color(0xFFF7F8FA),
        borderRadius: new BorderRadius.all(const Radius.circular(40))
      ),
      width: Dimension(context).toPx(349),
      height: Dimension(context).toPx(40),
      margin:  EdgeInsets.fromLTRB(Dimension(context).toPx(13), Dimension(context).toPx(16), Dimension(context).toPx(13), 0),
      padding: EdgeInsets.symmetric(horizontal: Dimension(context).toPx(16)),
    );
  }
}

class _CardState extends State {
  @override
  Widget build(BuildContext context){
    return new Container(
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          new Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('课表排课',style: TextStyle(color: Colors.white,fontSize: Dimension(context).toPx(18))),
              Text('提前了解课表课程',style: TextStyle(color: Colors.white,fontSize: Dimension(context).toPx(14))),
            ],
          ),
          new Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text('0',style: TextStyle(color: Colors.white,fontSize: Dimension(context).toPx(32))),
              Text('今日课程',style: TextStyle(color: Colors.white,fontSize: Dimension(context).toPx(14)))
            ],
          )
        ],
      ),
      width: Dimension(context).toPx(343),
      height: Dimension(context).toPx(108),
      margin: EdgeInsets.only(top: Dimension(context).toPx(24)),
      padding: EdgeInsets.all(Dimension(context).toPx(24)),
      decoration: new BoxDecoration(
        image: new DecorationImage(image: new AssetImage('assets/images/index-card-bg.png'),fit: BoxFit.cover),
        borderRadius: new BorderRadius.all(Radius.circular(Dimension(context).toPx(16)))
      ),
    );
  }
}

class _GridState extends State {
  @override
  Widget build(BuildContext context){
    return new Container(
      child: new Row(
        children: [
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: (){
              XlskRouter.go(context, '/student/list');
            },
            child: Container(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Image.asset('assets/images/index-grid-1.png',width: Dimension(context).toPx(32),height: Dimension(context).toPx(32)),
                  Text('学员',style: TextStyle(color: Color(0xFF1F2329),fontSize: Dimension(context).toPx(14)))
                ],
              ),
              width: Dimension(context).toVw(25,343),
            ),
          ),
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: (){
              XlskRouter.go(context, '/class/list');
            },
            child: Container(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Image.asset('assets/images/index-grid-2.png',width: Dimension(context).toPx(32),height: Dimension(context).toPx(32)),
                  Text('班级',style: TextStyle(color: Color(0xFF1F2329),fontSize: Dimension(context).toPx(14)))
                ],
              ),
              width: Dimension(context).toVw(25,343),
            ),
          ),
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: (){
              XlskRouter.go(context,'/classRecord');
            },
            child:Container(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Image.asset('assets/images/index-grid-3.png',width: Dimension(context).toPx(32),height: Dimension(context).toPx(32)),
                  Text('课程',style: TextStyle(color: Color(0xFF1F2329),fontSize: Dimension(context).toPx(14)))
                ],
              ),
              width: Dimension(context).toVw(25,343),
            ),
          ),
          Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Image.asset('assets/images/index-grid-4.png',width: Dimension(context).toPx(32),height: Dimension(context).toPx(32)),
                Text('作业',style: TextStyle(color: Color(0xFF1F2329),fontSize: Dimension(context).toPx(14)))
              ],
            ),
            width: Dimension(context).toVw(25,343),
          ),
        ],
      ),
      width: Dimension(context).toPx(343),
      height: Dimension(context).toPx(93),
      margin: EdgeInsets.only(top: Dimension(context).toPx(16)),
      padding: EdgeInsets.symmetric(vertical: Dimension(context).toPx(15)),
      decoration: new BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(Dimension(context).toPx(16)))
      ),
    );
  }
}

class _ClassRemind extends State<classRemind> {
  late List classType;

  @override
  void initState() {
    super.initState();

    classType = [{'title':'全部','isCheck':true},
      {'title':'未开课','isCheck':false},
      {'title':'进行中','isCheck':false},
      {'title':'已结课','isCheck':false}];
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
      child: new Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('今日课程提醒',style: TextStyle(color: Color(0xFF1F2329),fontSize: Dimension(context).toPx(18),fontWeight: FontWeight.w700)),
          Container(
            child: buildType(),
            margin: EdgeInsets.only(top: Dimension(context).toPx(16)),
          ),
          buildKecheng(),
        ],
      ),
      width: Dimension(context).toPx(343),
      margin: EdgeInsets.symmetric(vertical: Dimension(context).toPx(24)),
    );
  }

  Widget buildKecheng(){
    List _kechengList = widget.kechengList_rel;
    List<Widget> view = [];
    if(_kechengList.length > 0){
      for(var viewItem in _kechengList){
        view.add(
            GestureDetector(
              child: Container(
                child: Stack(
                  children: [
                    Container(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text('${viewItem['start_date']}',style: TextStyle(color: Color(0xFF8F959E))),
                          Container(
                            child: Row(
                              children: [
                                Text('${viewItem['timefrom']} - ${viewItem['timeto']}',style: TextStyle(color: Color(0xFF1F2329),fontSize: Dimension(context).toPx(23),fontWeight: FontWeight.w700)),
                                Container(
                                  child: Text('${viewItem['stu_num']??0}人',style: TextStyle(color: Color(0xFF1F2329),fontSize: Dimension(context).toPx(23),fontWeight: FontWeight.w700)),
                                  margin: EdgeInsets.only(left: Dimension(context).toPx(40)),
                                ),
                              ],
                            ),
                            margin: EdgeInsets.only(top: Dimension(context).toPx(6)),
                          ),
                          Container(
                            child: Row(
                              children: [
                                Text('${viewItem['jsname']}',style: TextStyle(color: Color(0xFF646A73),fontSize: Dimension(context).toPx(14))),
                                Container(
                                  child: Text('${viewItem['bjname']}',style: TextStyle(color: Color(0xFF646A73),fontSize: Dimension(context).toPx(14))),
                                  margin: EdgeInsets.only(left: Dimension(context).toPx(89)),
                                )
                              ],
                            ),
                            margin: EdgeInsets.only(top: Dimension(context).toPx(16)),
                          ),
                          Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text('写点评/作业',style: TextStyle(color: Color(0xFF1F2329),fontSize: Dimension(context).toPx(14))),
                                Icon(
                                  Icons.chevron_right,
                                  color: Color(0xFF646A73),
                                )
                              ],
                            ),
                            width: Dimension(context).toPx(311),
                            height: Dimension(context).toPx(43),
                            margin: EdgeInsets.only(top: Dimension(context).toPx(16)),
                            decoration: BoxDecoration(
                                border: Border(
                                    top: BorderSide(
                                        color: Color(0xFFEBEDF0),
                                        width: 1,
                                        style: BorderStyle.solid
                                    )
                                )
                            ),
                          ),
                        ],
                      ),
                      width: Dimension(context).toPx(343),
                      padding: EdgeInsets.fromLTRB(Dimension(context).toPx(16), Dimension(context).toPx(16), Dimension(context).toPx(16), 0),
                    ),
                    Positioned(child: Container(
                      child: Center(
                        child: Text('${filters_Kecheng_state(viewItem['state'])}',style: TextStyle(color: Color(viewItem['state']==1||viewItem['state']==2?0xFF1661FF:0xff8f959e),fontSize: Dimension(context).toPx(11),height: 1.1)),
                      ),
                      padding: EdgeInsets.all(Dimension(context).toPx(4)),
                      decoration: BoxDecoration(
                          color: Color(viewItem['state']==1||viewItem['state']==2?0xFFE6F2FF:0xfff7f8fa),
                          borderRadius: BorderRadius.all(Radius.circular(Dimension(context).toPx(4)))
                      ),
                    ),top: Dimension(context).toPx(16),right: Dimension(context).toPx(16),)
                  ],
                ),
                width: Dimension(context).toPx(343),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(Dimension(context).toPx(12)))
                ),
                margin: EdgeInsets.only(top:Dimension(context).toPx(18)),
              ),
              onTap:(){

              },
            )
        );
      }
    }else{
      view.add(
        Container(
          child: Center(
            child: Text('辛苦了，今天暂无课程安排',style: TextStyle(color: Color(0xFFC0C4CC),fontSize: Dimension(context).toPx(14))),
          ),
          width: Dimension(context).toPx(343),
          height: Dimension(context).toPx(167),
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(Dimension(context).toPx(12)))
          ),
          margin: EdgeInsets.only(top:Dimension(context).toPx(18)),
        )
      );
    }
    Widget content = Column(
      children: view,
    );
    return content;
  }

  Widget buildType(){ //生成classType视图数据
    List _classType = classType;
    List<Widget> typeView = [];
    for(var typeTtem in _classType){
      typeView.add(
        GestureDetector(
          child: AnimatedContainer(
            child: Text('${typeTtem['title']}',style: TextStyle(fontSize: Dimension(context).toPx(14),color: typeTtem['isCheck']?Color(0xFF1661FF):Color(0xFF1F2329),fontWeight: typeTtem['isCheck']?FontWeight.w700:FontWeight.w400)),
            decoration: new BoxDecoration(
              color: typeTtem['isCheck']?Color(0xFFE6F2FF):Color(0xFFF7F8FA),
              borderRadius: BorderRadius.all(Radius.circular(Dimension(context).toPx(15))),
            ),
            height: Dimension(context).toPx(24),
            padding: EdgeInsets.symmetric(horizontal: Dimension(context).toPx(15)),
            duration: Duration(milliseconds: 300),
          ),
          onTap: (){
            for(var o in _classType){
              o['isCheck'] = false;
            }
            typeTtem['isCheck'] = true;
            var idx = _classType.indexWhere((element) => element['isCheck']);
            List _kechengList;
            if(idx != 0){
              _kechengList = widget.kechengList.where((element) => element['state']==idx).toList();
            }else{
              _kechengList = widget.kechengList;
            }
            setState(() {
              classType = _classType;
              widget.kechengList_rel = _kechengList;
            });
          },
        )
      );
    }
    Widget content = Row(
      children: typeView,
    );
    return content;
  }

  // filters
  filters_Kecheng_state(state){
    switch(state){
      case 1:
        return "未开课";
      break;
      case 2:
        return "上课中";
      break;
      case 3:
        return "已结课";
      break;
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

}

